package user

import (
	"context"
	"fmt"

	"go-zero-admin/apps/user/api/internal/svc"
	"go-zero-admin/apps/user/api/internal/types"
	"go-zero-admin/pkg/config"
	"go-zero-admin/pkg/utils"

	"github.com/mojocn/base64Captcha"
	"github.com/satori/go.uuid"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetLoginStaticCaptchaLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetLoginStaticCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLoginStaticCaptchaLogic {
	return &GetLoginStaticCaptchaLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetLoginStaticCaptchaLogic) GetLoginStaticCaptcha() (resp *types.LoginStaticCaptchaResp, err error) {
	fmt.Println("===========> /login/staticCaptcha")
	store := base64Captcha.DefaultMemStore
	captcha := utils.NewCaptcha(45, 80, 4, 40, 30, 89, 0)
	driver := captcha.DriverString()
	id, b64s, err := base64Captcha.NewCaptcha(driver, store).Generate()
	val := store.Get(id, true)
	captchaId := uuid.NewV4().String()
	err = l.svcCtx.Redis.Setex(config.SysLoginCaptchaCachePrefix+captchaId, val, 600)

	return &types.LoginStaticCaptchaResp{
		CaptchaId:  captchaId,
		VerifyCode: b64s,
	}, nil
}
